package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-27
 */
@RestController
@RequestMapping("/admin/edu/teacher")
@Api(tags = "讲师管理")
@Slf4j
@CrossOrigin
public class AdminTeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation("分页讲师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                      @ApiParam("讲师列表查询对象") TeacherQuery teacherQuery){

//        Page<Teacher> pageParam = new Page<>(page, limit);
//        IPage<Teacher> pageModel = teacherService.page(pageParam);
//        return  R.ok().data("pageModel", pageModel);
        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQuery);
        return  R.ok().data("pageModel", pageModel);
    }

    @GetMapping("list")
    @ApiOperation("获取所有讲师列表")
    public R listAll(){
        log.info("当前打印日志级别{},时间为{}","info",new Date());
        log.error("当前打印日志级别{},时间为{}","error",new Date());
        log.warn("当前打印日志级别{},时间为{}","warn",new Date());
        log.debug("当前打印日志级别{},时间为{}","debug",new Date());

        List<Teacher> list = teacherService.list();
        System.out.println();
        return R.ok().data("items" , list);
    }

    @DeleteMapping("remove/{id}")
    @ApiOperation("根据ID删除讲师")
    public R removeById(@PathVariable String id){
        boolean result = teacherService.removeById(id);
        if (result) {
        return R.ok().message("删除成功！");
        }
        return R.error().message("没有找到此ID");
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("更新讲师")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if(result){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id获取讲师信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if(teacher != null){
            return R.ok().data("item", teacher);
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id列表删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(
            @ApiParam(value = "讲师id列表", required = true)
            @RequestBody List<String> idList){
        System.out.println("ID list===="+idList);
        boolean result = teacherService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
//        return R.ok();
    }
}

